package hongcaosp.app.android.contact;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SearchView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;

import java.util.List;

import hongcaosp.app.android.R;
import hongcaosp.app.android.comment.IViewType;
import hongcaosp.app.android.contact.friends.OnFriendClickListener;
import hongcaosp.app.android.login.UserToken;
import hongcaosp.app.android.modle.bean.IUser;
import hongcaosp.app.android.user.UserDetailActivity;
import xlj.lib.android.base.component.BaseActivity;
import xlj.lib.android.base.utils.StatusBarUtil;
import xlj.lib.android.base.view.ClearEditTextView;

public class ContactActivity extends BaseActivity implements IContactView ,OnFriendClickListener {
    private ClearEditTextView searchView;
    private RecyclerView recyclerView;
    private ContactAdapter contactAdapter;
    private ContactPresenter contactPresenter;
    @Override
    protected void initData(Intent intent) {
        contactPresenter = new ContactPresenter(this);
        contactPresenter.getContactList();
    }

    @Override
    protected void initView() {
        StatusBarUtil.setStatusBarDarkTheme(this,false);
        searchView = findViewById(R.id.search_bar);
        searchView.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                filter(s.toString());
            }
        });

        findViewById(R.id.back_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        recyclerView = findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        contactAdapter = new ContactAdapter(this, this);
        recyclerView.setAdapter(contactAdapter);
    }

    private void filter(String userName) {
        if (contactPresenter != null) {
            contactPresenter.filter(userName);
        }
    }

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_contact;
    }

    @Override
    public void freshContactList(List<IViewType> contactList) {
        contactAdapter.setData(contactList);
        contactAdapter.notifyDataSetChanged();
    }

    @Override
    public void onMessageClick(IUser user) {
        IMActivity.toIM(this,user.getUserName(),UserToken.getDefault().getUser().getId() + "",user.getUserId() + "");
    }

    @Override
    public void onItemClick(IUser user) {
        if (UserToken.getDefault().isLogged(this)) {
            IMActivity.toIM(this,user.getUserName(), UserToken.getDefault().getUser().getUserId() + "", user.getUserId() + "");
        }
    }
}
